package VWorldGameStudio;

import lombok.Getter;
import lombok.Setter;
import Function.FCMutilPlayPropulsionCache;

import java.awt.*;
import java.io.Serializable;
import java.util.LinkedList;

//牵引物接口
public abstract class FCPropulsion implements Serializable {


    @Getter
    private LinkedList<String> fcActionQueue = new LinkedList<>();

    public void inQueueAction() {
        String actionInfo = getPPianYiX() + "," + getPPianYiY() + "," + pcoorX + "," + pcoorY + "," + pdir;
        if (fcActionQueue.size() > 20) {
            fcActionQueue.pop();
        }
        fcActionQueue.addLast(actionInfo);
    }

    @Getter
    @Setter
    private String pmapname = "home2";// 所在地图名

    @Setter
    @Getter
    private int pcoorX = 0, pcoorY = 0;// 所在地图坐标
    @Setter
    @Getter
    private int pdir = 0;
    @Setter
    @Getter
    private int pPianYiX = 0, pPianYiY = 0;// 地图中心方块与画面中心的偏移量

    @Setter
    @Getter
    private int pactionNum = 0;

    @Setter
    @Getter
    public int beingTow = 0;

    @Setter
    @Getter
    public int visable = 1;//显示的开关，默认是1，0是关闭显示

    @Setter
    @Getter
    private String oldMapName;

    public void setPianYi(String action, String mapname){
        this.pmapname = mapname;
        String[] splits = action.split(",");
        pPianYiX = Integer.parseInt(splits[0]);
        pPianYiY = Integer.parseInt(splits[1]);
        pcoorX = Integer.parseInt(splits[2]);
        pcoorY = Integer.parseInt(splits[3]);
        pdir = Integer.parseInt(splits[4]);

        FCMutilPlayPropulsionCache.loacalShareIt(FCPropulsion.this);
    }

    public void setIsBeingTow(boolean flag){
        beingTow = flag?1:0;
        interSetIsBeingTow(flag);
    }

    public boolean isBeingTow(){
        return beingTow == 1;
    }
     abstract void interSetIsBeingTow(boolean flag);


    public abstract void onPropulsionChanged();
    public abstract boolean canTow();

    public abstract Image getPropulsionImage();

    public abstract String getPropulsionName();

    public abstract String getPropulsionType();

    public abstract String getPropulsionHostId();
    public abstract String getPropulsionID();

    public String toPString(){
        return "hostId:"+getPropulsionHostId()+" name:"+getPropulsionName()+" type:"+getPropulsionType()
                +" beingTow:"+beingTow+" pdir:"+pdir+" pNum:"+pactionNum;
    }

    public String getSaveString(){
        return pmapname+","+getPropulsionName()+","+getPropulsionHostId()+","+getPropulsionType()
                +","+pcoorX+","+pcoorY+","+pPianYiX+","+pPianYiY+","+beingTow+","+pdir+","+pactionNum+","+visable;
    }

    /**
     * 获取网络传输中的协议
     * @return
     */
    public byte[] getNetTransportString(){
        String string = getSaveString();
        byte[] bytes = string.getBytes();
        byte[] data = new byte[4+bytes.length];
        data[3] = (byte) (bytes.length & 0xff);
        data[2] = (byte) ((bytes.length & 0xff00) >> 8);
        data[1] = (byte) ((bytes.length & 0xff0000) >> 16);
        data[0] = (byte) ((bytes.length & 0xff000000) >> 24);
        System.arraycopy(bytes, 0,data, 4, bytes.length);
        return data;
    }


    //主动更新通知到共享的地方
//    public void notifyToSharePropulsinCahce(){
//        //TODO 还要发送出去
//        if (fcv.isSever()) {
//            FCRSocketServer.getIn().syncFCPropulsion(this);
//        } else if (fcv.isClient()) {
//            FCRSocketClient.getIn().syncFCPropulsion(this);
//        }
//    }
}
